package com.myteam.fugaocrm.sqldb.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;

import com.myteam.fugaocrm.modle.FgProject;
import com.myteam.fugaocrm.modle.ReportDTO;

public interface FgProjectMapper {
	
	/** 查询全部项目 */
	public List<FgProject> findFgProjectList(FgProject fgProject);

	//根据projectId查询项目明细java.lang.Integer
	@Select("SELECT p.project_id,p.project_name,p.customer_id, p.customer_company_id, p.staff_id, p.company_id,  "
			+ "p.offer, p.project_schedule, p.project_status, p.is_repair, p.expected_deal_date, p.repair_start_date, "
        + " p.repair_end_date,p.created_user, p.modified_user, p.created_date, p.modified_date,p.is_delete,p.note, "
		+ "(SELECT cm.customer_company_name FROM fg_customer_company cm WHERE cm.customer_company_id = p.customer_company_id AND cm.is_delete = 0) customerCompanyName, "
		+ "(SELECT GROUP_CONCAT(pm.partner_company_name) FROM fg_partner_company pm LEFT JOIN fg_partner_company_detail pd ON pm.partner_company_id =  pd.partner_company_id WHERE pd.project_id = p.project_id AND  pd.is_delete = 0  AND pm.is_delete = 0) partnerCompanyName, "
		+ "(SELECT s.staff_name FROM fg_staff s WHERE s.staff_id = p.staff_id AND s.is_delete = 0) staffName, "
		+ "CASE project_status WHEN '1'  THEN '项目机会' WHEN '2'  THEN '项目执行' WHEN '3'  THEN '项目废除' WHEN '4'  THEN '结项验收' ELSE '' END projectStatusValue "
		+ "FROM  fg_project p  WHERE p.is_delete = 0 and p.project_id = #{projectId}")
	public FgProject selectByProjectId(Integer projectId);
	
	
	//保存项目
	@Insert("INSERT into fg_project( project_name, customer_id, customer_company_id, staff_id, company_id, offer, project_schedule, project_status, expected_deal_date, created_user, modified_user, created_date, modified_date, is_delete) "
			+ "VALUES(#{projectName},#{customerId},#{customerCompanyId},#{staffId},#{companyId},#{offer},#{projectSchedule},#{projectStatus},#{expectedDealDate},#{createdUser},#{modifiedUser},#{createdDate},#{modifiedDate},#{isDelete});")
	public void saveProject(FgProject fgProject);
	
	//更新项目
	public void updateProjectSelective(FgProject fgProject);
	
	/** 查询售前项目机会(state=1) */
	@Select("SELECT * FROM fg_project WHERE project_status = #{state};")
	public List<FgProject> selectListByState(Integer state);
	
	/** 查询执行后期项目报表 */
	public List<FgProject> findImplReportList(ReportDTO reportDTO);
	
	/** 查询售前项目机会报表(state=1) */
	public List<FgProject> findBeforeReportList1(ReportDTO reportDTO);
	
	/** 查询售前项目机会报表(state=2) */
	public List<FgProject> findBeforeReportList2(ReportDTO reportDTO);
	
	/** 查询售前项目机会报表(state=2 or state=4) */
	public List<FgProject> findBeforeReportList24(ReportDTO reportDTO);
	
}
